package com.hospital.controller;

import com.hospital.common.R;
import com.hospital.pojo.Doctor;
import com.hospital.service.DoctorService;
import com.hospital.utils.GlobalUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@Slf4j
public class LoginController {

    @Resource
    private DoctorService doctorService;


    @RequestMapping({"","/","/login"})
    public String toLoginPage(Model model){
        return "sign-in";
    }

    @RequestMapping("/hospital/index")
    public String toIndexPage(){
        return "index";
    }

    @ResponseBody
    @PostMapping("/hospital")
    public R<Doctor> hospital(){
        String userName = GlobalUserUtil.getDoctor().toString();
        Doctor doctor = doctorService.getDoctorByInfo(userName);
        return R.success(doctor);
    }



    @GetMapping ("/userLogin")
    public String login(String userName,
                        String password,
                        HttpSession session,
                        Model model){
        if (userName.equals("") || password.equals("")){
            model.addAttribute("msg","请输入账号密码");
            return "sign-in";
        }
        try {
            if (!doctorService.selectByStatus(userName)){
                model.addAttribute("msg","用户已被禁用，请与管理员联系！");
                return "sign-in";
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
            model.addAttribute("msg","该用户不存在！");
            return "sign-in";
        }
        // 1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        // 2.封装请求数据到token
        AuthenticationToken token = new UsernamePasswordToken(userName,password);
        //  3.调用login方法进行登录认证
        try {
            subject.login(token);
            session.setAttribute("user",token.getPrincipal().toString());
            System.out.println("--------------------"+GlobalUserUtil.getDoctor());
            return "redirect:/hospital/index";
        }
        catch(UnknownAccountException e){
            model.addAttribute("msg","用户不存在");
            return "sign-in";
        }
        catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "sign-in";
        }
        catch (AuthenticationException e) {
            e.printStackTrace();
            model.addAttribute("msg","登陆失败，请重试！");
            return "sign-in";
        }
    }

    // 退出登录
    @ResponseBody
    @GetMapping("/hospital/logout")
    public R logout(){
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            subject.logout();
            return R.success(1,"退出登录成功！");
        }
        return R.error("退出登录失败！");
    }

}